<?php

/**
 * Description of Default_Model_User
 *
 * @author Trung Dung
 * @version 3.0
 */
class Default_Model_User extends MySQL {

    private $_cache;
    protected static $_instance = null;

    private function __clone() {
        
    }

    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Table user
     * @var TABLE SQL
     */
    protected $_name = Utilities_Constant::TABLE_USER;

    public function __construct() {
        $this->_cache = Zend_Registry::get("cache_data");
        $config = Zend_Registry::get("dbOption");
        $this->setConection($config);
        $this->setFetchMode("FETCH_OBJ");
        $this->connect();
    }

    public function __destruct() {
        $this->disconnect();
    }

    /**
     * Lấy thông tin dựa vào var đưa vào
     * Với $recordPerPage = 0 sẽ lấy toàn bộ record
     * @param integer $currentPages
     * @param integer $recordPerPage
     * @param array $selectField
     * @param text $Where
     * @param text $OrderBy
     * @param text $SortBy
     * @return array 
     */
    public function getAll($currentPages, $recordPerPage, $SelectField = '*', $Where = null, $OrderBy = 'user_id', $SortBy = 'DESC') {
        try {
            $offset = ($currentPages - 1) * $recordPerPage;
            
            if ($SelectField != "*")
                $SelectField = implode(", ", $SelectField);
            if ($Where)
                $Where = "WHERE $Where";
            if ($OrderBy)
                $OrderBy = "ORDER BY $OrderBy $SortBy";
            if ($recordPerPage)
                $Limit = "LIMIT $offset,$recordPerPage"; else
                $Limit = "";
            $row = $this->fetchAll("SELECT $SelectField FROM $this->_name $Where $OrderBy $Limit");
            return $row;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }
    
    //Đếm số thành viên
    public function count(){
        $sql = "select count(user_id) as count from $this->_name";
        $row = $this->fetchRow($sql);
        return (int)$row->count;
    }
    

    /**
     * Lấy thông tin của user dựa vào user_id
     * @param integer $userId
     * @return Object
     */
    public function getUserById($userId) {
        try {
            if ($this->_cache->load('user_' . $userId) === false) {
                $row = $this->fetchRow("SELECT * FROM $this->_name WHERE user_id = '" . (int) $userId . "'");
                if ($row)
                    $this->_cache->save($row, 'user_' . $userId);
            } else
                $row = $this->_cache->load('user_' . $userId);
            return $row;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    /**
     * Lấy thông tin của user dựa vào user_name
     * @param integer $userId
     * @return Object
     */
    public function getUserByUsername($userName) {
        try {
            if ($this->_cache->load('user_name_' . preg_replace("/\./", "", $userName)) === false) {
                $row = $this->fetchRow("SELECT * FROM $this->_name WHERE user_name = '" . $userName . "'");
                if ($row)
                    $this->_cache->save($row, 'user_name_' . preg_replace("/\./", "", $userName));
            } else
                $row = $this->_cache->load('user_name_' . preg_replace("/\./", "", $userName));
            return $row;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }
    
    /**
     * Lấy thông tin của user dựa vào user_name
     * @param integer $userId
     * @return Object
     */
    public function getUserByEmail($email) {
        try {
            if ($this->_cache->load('user_email_' . preg_replace("/\./", "", $email)) === false) {
                $row = $this->fetchRow("SELECT * FROM $this->_name WHERE user_email = '" . $email . "'");
                if ($row)
                    $this->_cache->save($row, 'user_email_' . preg_replace("/\./", "", $email));
            } else
                $row = $this->_cache->load('user_email_' . preg_replace("/\./", "", $email));
            return $row;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }
    
 
    /**
     * Lấy thông tin của user dựa vào user_mobile
     * @param integer $userId
     * @return Object
     */
    public function getUserByMobile($mobile) {
        try {
            $row = $this->fetchRow("SELECT * FROM $this->_name WHERE user_mobile = '" . $mobile . "'");
            return $row;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    /**
     * Insert thông tin user
     * @param array $userData
     * @return integer 
     */
    public function insertUser($userData = array()) {
        try {
            return $this->insert($this->_name, $userData);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    /**
     * Cập nhật dữ liệu cho User
     * @param array $data
     * @param integer $userId
     * @return boolean 
     */
    public function updateUser($userData = array(), $userId) {
        try {
            $user = $this->getUserById($userId);
            $this->_cache->remove('user_' . $userId);
            $this->_cache->remove('user_name_' . preg_replace("/\./", "", $user->user_name));
            $updateUser = $this->update($this->_name, $userData, "user_id = '" . (int) $userId . "'");
            return $updateUser;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    /**
     * Xóa user khỏi CSDL
     * @param integer $userId
     * @return boolean 
     */
    public function deleteUser($userId) {
        try {
            $user = $this->getUserById($userId);
            $this->_cache->remove('user_' . $userId);
            $this->_cache->remove('user_name_' . preg_replace("/\./", "", $user->user_name));
            return $this->delete($this->_name, "user_id = '" . (int) $userId . "'");
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

}